package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import as3sdk.utils.UtilProtoClientActionScriptParse;
	import as3sdk.utils.UtilXMLParse;
	import as3sdk.utils.proto.BuildTemplateObject;
	import as3sdk.utils.proto.ObjectType;
	
	/**
	 * 
	 * @author hanxianming
	 * 
	 */	
	public class ProtoTransformTool extends Sprite
	{
		private var _fileList:Array;
		
		private var _urlLoader:URLLoader;
		
		private var _urlRequest:URLRequest;
		
		private var _protoList:Vector.<Object>;
		
		private var _buildObjectList:Vector.<BuildTemplateObject>;
		
		public function ProtoTransformTool()
		{
			initizlize();
		}
		
		private function initizlize():void
		{
			
			_urlRequest = new URLRequest();
			
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			_urlLoader.addEventListener(Event.COMPLETE, urlLoaderCompleteHandler);
			
			
			_protoList = new Vector.<Object>();
			
			_buildObjectList = new Vector.<BuildTemplateObject>();
			
			initFileList();
		}
		
		private function initFileList():void
		{
			var tempFile:File;
			tempFile = File.applicationDirectory;
			tempFile.url = tempFile.url + "protoConfig";
			
			try
			{
				_fileList = tempFile.getDirectoryListing();
			} 
			catch(error:Error) 
			{
				trace("[error] File.getDirectoryListing() error  " + tempFile.nativePath);
				return;
			}
			
			loadFileList();
		}
		
		private function loadFileList():void
		{
			if (_fileList.length <= 0)
			{
				fileListLoadAllComplate()
				return;
			}
			
			var tempFile:File;
			
			tempFile = _fileList.pop();
			
			if (tempFile.isDirectory)
			{
				loadFileList();
				return;
			}
			
			_urlRequest.url = tempFile.url;
			
			_urlLoader.load(_urlRequest);
		}
		
		protected function urlLoaderCompleteHandler(event:Event):void
		{
			var xml:XML = new XML(_urlLoader.data);
			var configObject:Object = UtilXMLParse.xml2Object(xml);
			
			_protoList.push(configObject);
			
			loadFileList();
			
		}
		
		private function fileListLoadAllComplate():void
		{
			var len:int = _protoList.length;
			var object:Object;
			for (var i:int = 0; i < len; i++) 
			{
				object = _protoList[i];
				
				for each (var itemObject:Object in object) 
				{
					if (itemObject is String)
						continue;
					
					buildObjectList(itemObject, true);
				}
			}
			
			buildMsgSource();
		}
		
		private function buildObjectList(object:Object, root:Boolean = false):void
		{
			var template:BuildTemplateObject = new BuildTemplateObject();
			_buildObjectList.push(template);
			
			var className:String = object.className;
			var propType:String = className.slice(className.lastIndexOf(".") + 1);
			var packageString:String = className.slice(0, className.lastIndexOf("."));
			var comment:String = object.comment;
			template.msgTypeProp = object.msgTypeProp;
			template.msgTypeValue = parseInt(object.msgTypeValue, 16);
			template.className = propType;
			template.packageString = packageString;
			template.comment = comment;
			template.objectType = UtilProtoClientActionScriptParse.getObjectType(object.type);
			
			for each (var item:Object in object) 
			{
				if (item is String)
					continue;
				
				var objectType:int = UtilProtoClientActionScriptParse.getObjectType(item.type);
				if (objectType == ObjectType.BUILD_OBJECT_TYPE)
				{
					template.addVariables(item.name, item.type, item.type.slice(item.type.lastIndexOf(".") + 1), item.comment, item.sizeByteType);
				}
				else
				{
					template.addVariables(item.name, item.type, item.byteType, item.comment, item.sizeByteType);
				}
			}
		}
		
		private function buildMsgSource():void
		{
			
			UtilProtoClientActionScriptParse.initBuildObjectList(_buildObjectList);
			
			var len:int = _buildObjectList.length;
			var buildObject:BuildTemplateObject;
			var tempString:String;
			var tempUrl:String;
			for (var i:int = 0; i < len; i++) 
			{
				buildObject = _buildObjectList[i];
				
				if (buildObject.objectType == ObjectType.BUILD_PROTO_SC_TYPE)
				{
					tempString = UtilProtoClientActionScriptParse.buildSCProtoObject(buildObject);
					tempUrl = String(buildObject.packageString + "." + buildObject.className).replace(/\./g, File.separator);
					saveFile(tempString, tempUrl + ".as");
				}
				else if(buildObject.objectType == ObjectType.BUILD_PROTO_CS_TYPE)
				{
					tempString = UtilProtoClientActionScriptParse.buildCSProtoObject(buildObject);
					tempUrl = String(buildObject.packageString + "." + buildObject.className).replace(/\./g, File.separator);
					saveFile(tempString, tempUrl + ".as");
				}
				else
				{
					tempString = UtilProtoClientActionScriptParse.buildNormalObject(buildObject);
					tempUrl = String(buildObject.packageString + "." + buildObject.className).replace(/\./g, File.separator);
					saveFile(tempString, tempUrl + ".as");
				}
			}
			
			trace("[info] all completed......" + new Date().toString());
		}
		
		private function saveFile(templateString:String, fileName:String):void
		{
			var fileStream:FileStream = new FileStream();
			
			try 
			{
				var tempFile:File = File.applicationDirectory;
				tempFile.nativePath = tempFile.nativePath + File.separator + fileName;
				fileStream.open(tempFile, FileMode.WRITE);
				fileStream.writeUTFBytes(templateString);
				fileStream.close();
				
			}catch (e:Error) 
			{
				trace("[error] fileStream error：" + tempFile.nativePath);
			}
			fileStream = null;
			
			trace("[info] " + fileName + " complete.....");
		}
	}
}